<?php


namespace app\admin\controller;
use app\common\BaseHelper;
use app\admin\model\User;
use app\common\model\Ordercommission;
use app\common\model\Usercommission;

use think\Request;

class SaleMember extends Base{

    public function index(){
        // dump(session('TENANT_ID'));die;
        return $this->fetch();
    }

    public function indexApi(){
        //从user表中查询分销用户，chiefUserId
        $request = Request::instance();
        $param = $request->param();
        $where = ['tenantId'=>session('TENANT_ID')];

        $start = $param['start'];
        $length = $param['length'];

        $user = User::where($where)->where('chiefUserId','not null')->select();
        $userId = [];
        foreach ($user as $u) {
            if($u['chiefUserId']){
                $userId[] = $u['chiefUserId'];
            }
        }

        $where['id'] = ['in',implode(',', $userId)];
        if($param['search']['value']) {  //拼接搜索条件
            $ss = json_decode($param['search']['value'],true);
            if($ss['search'])
                $where['nickName|mobile'] = ['like','%'.$ss['search'].'%'];
        }
        $list = User::where($where)->limit($start,$length)->select();

        $count = User::where($where)->count();
        $res = [
            'status' => '1',
            'draw' => $param['draw'],
            'data' => $list,
            'recordsFiltered' => $count,
            'recordsTotal' => $count,
        ];
        return json($res);
    }

    //单个分销人员的收益记录
    public function profit() {
        $request = Request::instance();
        $id = $request->param('userId');
        $user = User::field('id,name,nickName,totalIncome')->where('id',$id)->find();
        $this->assign('user',$user);
        return $this->fetch();
    }

    public function profitApi() {
        $request = Request::instance();
        $param = $request->param();
        $where = ['shangjiId'=>$param['userId']];

        $start = $param['start'];
        $length = $param['length'];

        $join = [
            ['__USER__ u', 'a.userId = u.id'],
            ['__ORDER__ o', 'a.orderId = o.id']
        ];
        $fields = 'a.*, u.nickName userName, o.orderNO, o.total, o.total_count, o.completedtime';
        $list = Ordercommission::alias('a')->join($join)->field($fields)->where($where)->limit($start, $length)->select();
        /*foreach ($list as $k => $v) {
            $list[$k]['profit'] = $v['total'] * ($v['proportion']/100);
        }*/

        $count = Ordercommission::where($where)->count();
        $res = [
            'status' => '1',
            'draw' => $param['draw'],
            'data' => $list,
            'recordsFiltered' => $count,
            'recordsTotal' => $count
        ];
        return json($res);
    }

    public function commission() {
        $request = Request::instance();
        $id = $request->param('userId');
        $user = User::field('id,name,nickName,totalWithdrawals,directBindNums')->where('id',$id)->find();
        $this->assign('user',$user);
        return $this->fetch();
    }

    public function commissionApi() {
        $request = Request::instance();
        $param = $request->param();
        $where = ['userId'=>$param['userId'],'status'=>$param['status']];

        $start = $param['start'];
        $length = $param['length'];

        $list = Usercommission::where($where)->select();

        $count = Usercommission::where($where)->count();
        $res = [
            'status' => '1',
            'draw' => $param['draw'],
            'data' => $list,
            'recordsFiltered' => $count,
            'recordsTotal' => $count
        ];
        return json($res);
    }


}